apiready = function() {
    seajs.use(['../../V', "../../api", "../../jquery", "../../app", "../../service", "../../swiper", "../../dialog"], function(V, $api, $, app, service, Swiper, dialog) {
        var el = document.querySelector('.container');
        var vm = {
            el: el,
            data: {
                userInfo: '', // 获取用户身份
                unionid: '',  //用户unionid
                isShowBlock: false, //是否显示任务提示
                src: '',
                MemberGoodsData:{}//升级会员的任务
            },
            created: function() {
                this.init();
            },
            mounted: function() {
                var self = this;
                api.addEventListener({
                    name: 'login_flag'
                }, function() {
                    self.init()
                });

                //--下拉刷新
                app.refreshHeaderInfo(function() {
                    self.init();
                });
            },
            methods: {
                init: function() {
                    var that = this;

                    if ($api.getStorage("user")) {
                        this.userInfo = $api.getStorage("user").data;
                        this.src = "../../../image/member/star_level/"+this.userInfo.star+".png";
                        this.unionid = $api.getStorage("shop_unionid");
                        app.checkWXLogin(dialog);
                        this.getMemberTask()
                        // this.openFm()
                        // 预先打开店主任务
                        this.openUpgrade_fm(0,function(){
                          api.setFrameAttr({
                              name: 'upgrade_fm',
                              rect: {
                                  x: 0,
                                  y: 0,
                                  w: api.winWidth,
                                  h: api.winHeight
                              },
                              hidden:true
                          });
                        })
                    }
                },
                toMyShop: function(){
                  api.openWin({
                      name: 'single_shop',
                      url: '../shop_index/single_shop.html',
                      pageParam: {
                          out_user_id: $api.getStorage('user').data.out_user_id
                      },
                      reload: true
                  })
                },
                // 获取用户信息
                getUserInfo:function(cb){
                  app.ajax(
                      service['getUserShopInfo'], {
                          data: {
                              values: {
                                  appid: app.appid,
                                  userid: $api.getStorage("user").data.out_user_id,
                                  mask: 'none',
                                  bs: ''
                              }
                          }
                      },
                      function(res) {
                          if (res.code == 0) {
                              $api.setStorage("shop_unionid", res.data.shop_unionid);
                              $api.setStorage("shop_data", res.data);
                              $api.setStorage("user", res);
                              cb();
                          }
                      })
                },
                // 前往商品详情
                toGoods_detail:function (goods_id) {
                  api.openWin({
                      name: 'goods_detail',
                      url: '../buy_goods/goods.html',
                      pageParam: {
                          goodsId: goods_id,
                          isSeller:0
                      },
                      reload:true
                  });
                },
                // 升级成功
                openFm:function(){
                  api.openFrame({
                      name: 'member_fm',
                      url: './member_fm.html',
                      rect: {
                          x: 0,
                          y: 0,
                          w: api.winWidth,
                          h: api.winHeight
                      },
                      bounces: false,
                      bgColor: 'rgba(0,0,0,.3)'
                  });

                },
                // 查看是否还存在任务
                getMemberTask:function(){
                  var that = this
                  app.ajax(service.getMemberTask, {
                      data: {
                          values: {
                              unionid: that.unionid,
                              bs: "",
                              mask:"none"
                          }
                      }
                  }, function(res) {
                    /*
                    *already_buy_num:完成任务数
                    *total_need_buy:任务数量
                    *has_tasking:是否还有任务
                    */
                    if(res.code==0){
                      if(res.data.has_tasking==1){
                       that.isShowBlock = true;
                     }else{
                       that.isShowBlock = false;
                     }
                    }
                  })
                },
                //获取会员商品
                getMemberGoods: function(e){
                  if($api.getStorage('user')){
                    var that = this;
                    if(!$api.getStorage('user').data.higher.out_user_id){//没有上级填写邀请码
                      api.sendEvent({
                          name: 'openDialog'
                      });
                    }else{
                      api.setFrameAttr({
                          name: 'upgrade_fm',
                          rect: {
                              x: 0,
                              y: 0,
                              w: api.winWidth,
                              h: api.winHeight
                          },
                          hidden:false
                      });
                      api.sendEvent({
                          name: 'getMemberGoods'
                      });
                    }
                  }else{
                    api.confirm({
                      title: '提示',
                      msg: '您当前还未登录，请前往登录',
                      buttons: ['确定', '取消']
                  }, function(ret, err) {
                    if(ret.buttonIndex==1){
                        api.closeWin()
                    }
                  });
                  }
                },
                openUpgrade_fm:function(height,cb){
                  api.openFrame({
                      name: 'upgrade_fm',
                      url: './upgrade_fm.html',
                      rect: {
                          x: 0,
                          y: 0,
                          w: api.winWidth,
                          h: height
                      },
                      pageParam:{
                        unionid:this.unionid,
                      },
                      bounces: false,
                      bgColor: 'rgba(0,0,0,.5)'
                  });
                  if(cb){
                    cb
                  }
                },
                closeUpgrade: function(){
                  this.show_upgrade  = false;
                },
            }
        }
        var even = function() {
          api.addEventListener({
              name: 'getUserInfo'
          }, function(ret, err){
            newVue.getUserInfo(function(){
              newVue.init()
            })
          });
          api.addEventListener({
              name: 'memberINIT'
          }, function(ret, err){
              newVue.init()
          });

          api.addEventListener({
              name: 'getStar'
          }, function(ret, err){
              newVue.getMemberTask()
          });
        }
        var newVue = new Vue(vm);
        even();
        // V.apiready(vm,even);
    });
}
